package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "checkgroup")
@Slf4j
public class CheckGroupController {


    @DubboReference
    CheckGroupService checkGroupService;


    /**
     * @Author: JASON
     * @Date: 16:17 2021/1/4
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查组分页查询
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findByPage")
    public Result findByPage(QueryPageBean queryPageBean){
        log.info("Controller分页查询检查组"+queryPageBean);
        PageResult pageResult = checkGroupService.findByPage(queryPageBean);
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }
    /**
     * @Author: JASON
     * @Date: 17:08 2021/1/4
     * @Parms [checkGroup]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 新增检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @PostMapping("add")
   public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        log.info("[检查组-添加]data:{},checkitemIds:{}",checkGroup,checkItemIds);
        //RPC请求添加
        checkGroupService.add(checkGroup,checkItemIds);
        return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: JASON
     * @Date: 17:08 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id查询检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("Controller检查组根据id查询" + id);
        CheckGroup checkGroup=checkGroupService.findById(id);
        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * @Author: JASON
     * @Date: 17:12 2021/1/4
     * @Parms [checkGroup]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id修改检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @PostMapping("edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("[检查组-编辑]data：{},checkitemIds:{}",checkGroup,checkitemIds);
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * @Author: JASON
     * @Date: 17:13 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id删除检查组
    */
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @GetMapping("delete")
    public Result delete(Integer id){
        log.info("Controller根据id删除检查组"+id);
        checkGroupService.delete(id);
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * @Author: JASON
     * @Date: 15:50 2021/1/5
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据groupid查询checkitemID
    */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("Controller根据groupid查询checkitemID"+id);
        List<Integer> list=checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, "根据组id查询项id成功", list);
    }


    /**
     * @Author: JASON
     * @Date: 9:27 2021/1/6
     * @Parms []
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 查询所有
    */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("Controller[检查组]查询所有");
        List<CheckGroup> list =checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,list);
    }
}
